package com.bdqn.controller;

import com.bdqn.pojo.User;
import com.bdqn.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * LoginController
 *
 * @author LILIBO
 * @since 2023-12-11
 */
@Controller
public class LoginController {

    @Resource
    private UserService userService;

    /**
     * 去登录页面
     *
     * @return
     */
    @GetMapping("/login")
    public String toLogin() {
        System.out.println("去登录页面");
        return "login";
    }

    /**
     * 执行登录操作
     *
     * @param usrName
     * @param usrPassword
     * @return
     */
    @PostMapping("/login")
    public String doLogin(Model model, HttpSession session, String usrName, String usrPassword) {
        System.out.println("执行登录操作");
        User loginUser = userService.login(usrName, usrPassword);
        if (loginUser == null) {
            model.addAttribute("message", "登录失败，账号或密码错误。");
            return "login";
        }
        session.setAttribute("loginUser", loginUser);
        return "redirect:/main";
    }

}
